package cn.virens.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class RequestUtil {

	/**
	 * 获取request中的所有值
	 * 
	 * @return
	 */
	public static Map<String, String[]> getParam(HttpServletRequest request) {
		return request.getParameterMap();
	}

	/**
	 * 根据默认值返回对应的类型值
	 * 
	 * @param columnName 字段名
	 * @param defaultValue 默认值
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getValue(HttpServletRequest request, String columnName, T defaultValue) {
		if (defaultValue instanceof String) {
			return (T) getValueStr(request, columnName, (String) defaultValue);
		} else if (defaultValue instanceof Integer) {
			return (T) getValueInt(request, columnName, (Integer) defaultValue);
		} else if (defaultValue instanceof Boolean) {
			return (T) getValueBoolean(request, columnName, (Boolean) defaultValue);
		} else if (defaultValue instanceof Float) {
			return (T) getValueFloat(request, columnName, (Float) defaultValue);
		} else if (defaultValue instanceof Long) {
			return (T) getValueLong(request, columnName, (Long) defaultValue);
		} else {
			return defaultValue;
		}
	}

	/**
	 * 返回对应的类型
	 * 
	 * @param columnName 字段名
	 * @param classname 类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getValue(HttpServletRequest request, String columnName, Class<T> classname) {
		if (classname.equals(String.class)) {
			return (T) getValueStr(request, columnName);
		} else if (classname.equals(Integer.class)) {
			return (T) getValueInt(request, columnName);
		} else if (classname.equals(Boolean.class)) {
			return (T) getValueBoolean(request, columnName);
		} else if (classname.equals(Float.class)) {
			return (T) getValueFloat(request, columnName);
		} else if (classname.equals(Long.class)) {
			return (T) getValueLong(request, columnName);
		} else {
			return null;
		}
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @param request
	 * @return
	 */
	public static String[] getValue(HttpServletRequest request, String columnName) {
		String[] values = request.getParameterValues(columnName);
		return (values != null) ? values : new String[] {};
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static String getValueStr(HttpServletRequest request, String columnName) {
		return getValueStr(request, columnName, null);
	}

	/**
	 * 在request中根据name查找值,如找不到就返回默认值
	 * 
	 * @param columnName
	 * @param defaultValue
	 * @return
	 */
	public static String getValueStr(HttpServletRequest request, String columnName, String defaultValue) {
		String sValue = request.getParameter(columnName);
		if (sValue == null || sValue.length() == 0) {
			return defaultValue;
		} else {
			return sValue;
		}
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static Integer getValueInt(HttpServletRequest request, String columnName) {
		return getValueInt(request, columnName, 0);
	}

	/**
	 * 在request中根据name查找值,如找不到就返回默认值
	 * 
	 * @param columnName
	 * @param defaultValue
	 * @return
	 */
	public static Integer getValueInt(HttpServletRequest request, String columnName, Integer defaultValue) {
		return NumberUitl.parseInt(getValueStr(request, columnName), defaultValue);
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static Long getValueLong(HttpServletRequest request, String columnName) {
		return getValueLong(request, columnName, 0L);
	}

	/**
	 * 在request中根据name查找值,如找不到就返回默认值
	 * 
	 * @param columnName
	 * @param defaultValue
	 * @return
	 */
	public static Long getValueLong(HttpServletRequest request, String columnName, Long defaultValue) {
		return NumberUitl.parseLong(getValueStr(request, columnName), defaultValue);
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static Double getValueDouble(HttpServletRequest request, String columnName) {
		return getValueDouble(request, columnName, 0D);
	}

	/**
	 * 在request中根据name查找值,如找不到就返回默认值
	 * 
	 * @param columnName
	 * @param defaultValue
	 * @return
	 */
	public static Double getValueDouble(HttpServletRequest request, String columnName, Double defaultValue) {
		return NumberUitl.parseDouble(getValueStr(request, columnName), defaultValue);
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static Float getValueFloat(HttpServletRequest request, String columnName) {
		return getValueFloat(request, columnName, 0F);
	}

	/**
	 * 在request中根据name查找值,如找不到就返回默认值
	 * 
	 * @param columnName
	 * @param defaultValue
	 * @return
	 */
	public static Float getValueFloat(HttpServletRequest request, String columnName, Float defaultValue) {
		return NumberUitl.parseFloat(getValueStr(request, columnName), defaultValue);
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static Boolean getValueBoolean(HttpServletRequest request, String columnName) {
		return getValueBoolean(request, columnName, false);
	}

	/**
	 * 在request中根据name查找值,如找不到就返回默认值
	 * 
	 * @param columnName
	 * @param defaultValue
	 * @return
	 */
	public static Boolean getValueBoolean(HttpServletRequest request, String columnName, Boolean defaultValue) {
		String sValue = getValueStr(request, columnName, null);
		if ("true".equals(sValue) || "1".equals(sValue) || "on".equals(sValue)) {
			return true;
		} else if ("false".equals(sValue) || "0".equals(sValue)) {
			return false;
		} else {
			return defaultValue;
		}
	}

	/**
	 * 获取 - 时间
	 * 
	 * @param columnName 变量名
	 * @return Date对象
	 */
	public static Date getValueDate(HttpServletRequest request, String columnName) {
		return getValueDate(request, columnName, "yyyy-MM-dd HH:mm:ss", null);
	}

	/**
	 * 获取 - 时间
	 * 
	 * @param request
	 * @param columnName 变量名
	 * @param format 时间格式
	 * @return Date对象,默认当前时间
	 */
	public static Date getValueDate(HttpServletRequest request, String columnName, String format) {
		return getValueDate(request, columnName, format, new Date());
	}

	/**
	 * 获取 - 时间
	 * 
	 * @param request
	 * @param columnName 变量名
	 * @param format 时间格式
	 * @param defaultValue 默认值
	 * @return Date对象
	 */
	public static Date getValueDate(HttpServletRequest request, String columnName, String format, Date defaultValue) {
		String sValue = getValueStr(request, columnName);
		if (sValue != null) {
			try {
				return (new SimpleDateFormat(format)).parse(sValue);
			} catch (ParseException e) {
			}
		}
		return defaultValue;
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static JSONObject getValueJSON(HttpServletRequest request, String columnName) {
		return JSON.parseObject(getValueStr(request, columnName));
	}

	/**
	 * 在request中根据name查找值
	 * 
	 * @param columnName 参数名
	 * @return
	 */
	public static List<String> getValueForList(HttpServletRequest request, String columnName) {
		return Arrays.asList(getValue(request, columnName));
	}

	public static String getContentType(HttpServletRequest request) {
		return request.getContentType();
	}

	/**
	 * 获取请求HTTP头
	 * 
	 * @param header
	 * @return
	 */
	public static String getHeader(HttpServletRequest request, String header) {
		return request.getHeader(header);
	}

	/**
	 * 获取请求类型
	 * 
	 * @return
	 */
	public static String getMethod(HttpServletRequest request) {
		return request.getMethod();
	}

	/**
	 * 获取Session
	 * 
	 * @return
	 */
	public static HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}

	/**
	 * 获取根据名字获取Session值
	 * 
	 * @param name
	 * @return
	 */
	public static Object getSession(HttpServletRequest request, String name) {
		return getSession(request).getAttribute(name);
	}

	/**
	 * 获取Cookie
	 * 
	 * @return
	 */
	public static Cookie[] getCookies(HttpServletRequest request) {
		return request.getCookies();
	}

	/**
	 * 获取根据名字获取Cookie值
	 * 
	 * @param name
	 * @return
	 */
	public static String getCookies(HttpServletRequest request, String name) {
		Cookie[] cookies = getCookies(request);
		for (Cookie cookie : cookies) {
			if (name.equals(cookie.getName())) {
				return cookie.getValue();
			}
		}
		return null;
	}

	/**
	 * 获取当前请求地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getQueryUrl(HttpServletRequest request) {
		return request.getRequestURI() + (request.getQueryString() != null ? ("?" + request.getQueryString()) : "");
	}

	/**
	 * 获取工程路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getRealPath(HttpServletRequest request) {
		return getRealPath(request, null);
	}

	/**
	 * 根据request获取相对路径在web项目中的完整地址 request请求
	 * 
	 * @param filepath 项目中的相对于contextPath的相对路径
	 * @return 项目所在路径 + 相对路径的完整地址
	 */
	public static String getRealPath(HttpServletRequest request, String filepath) {
		String realBasePath = request.getServletContext().getRealPath("/");
		if (filepath != null) {
			if (!filepath.startsWith("/")) {
				return realBasePath + "/" + filepath;
			} else {
				return realBasePath + filepath;
			}
		}
		return realBasePath;
	}
}
